<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/17
 * Time: 14:41
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Food as FoodStore;
class Food extends Nocommon
{
    /*
     * 店铺美食列表
     */
    public function foodList()
    {
        if(Request::instance()->isPost())
        {
            $type = input('type','1','trim');
            $sort = input('sort','1','trim');
            $content = input('content','','trim');

            $where['food_store_name'] = array('like','%' . $content . '%');

            $post = input('param.');
            $page = $this->p();
            $food = new FoodStore();
            //按距离排序
            if($type==1 && $sort==1){
                $foodStore = $food->food_list($where,$page,1,$post['longitude'],$post['latitude']);
            }
            if($type==2 && $sort==1){
                $where['food_store_tag'] = array('like','%'. 1 .'%');
                $foodStore = $food->food_list($where,$page,1,$post['longitude'],$post['latitude']);
            }
            if($type==3 && $sort==1){
                $where['food_store_tag'] = array('like','%'. 2 .'%');
                $foodStore = $food->food_list($where,$page,1,$post['longitude'],$post['latitude']);
            }
            if($type==4 && $sort==1){
                $where['food_store_tag'] = array('like','%'. 3 .'%');
                $foodStore = $food->food_list($where,$page,1,$post['longitude'],$post['latitude']);
            }
            if($type==5 && $sort==1){
                $where['food_store_tag'] = array('like','%'. 4 .'%');
                $foodStore = $food->food_list($where,$page,1,$post['longitude'],$post['latitude']);
            }
            //按价格排序
            if($type==1 && $sort==2){
                $foodStore = $food->food_list($where,$page,2,$post['longitude'],$post['latitude']);
            }
            if($type==2 && $sort==2){
                $where['food_store_tag'] = array('like','%'. 1 .'%');
                $foodStore = $food->food_list($where,$page,2,$post['longitude'],$post['latitude']);
            }
            if($type==3 && $sort==2){
                $where['food_store_tag'] = array('like','%'. 2 .'%');
                $foodStore = $food->food_list($where,$page,2,$post['longitude'],$post['latitude']);
            }
            if($type==4 && $sort==2){
                $where['food_store_tag'] = array('like','%'. 3 .'%');
                $foodStore = $food->food_list($where,$page,2,$post['longitude'],$post['latitude']);
            }
            if($type==5 && $sort==2){
                $where['food_store_tag'] = array('like','%'. 4 .'%');
                $foodStore = $food->food_list($where,$page,2,$post['longitude'],$post['latitude']);
            }
            return $this->apiReturn('200','成功',$foodStore);
        }
    }
           /**
     * [getTourInfo 获取美食店铺的详情]
     * @Author   kendy
     * @DateTime 2019-10-30T9:06:35+0800
     * @return   [type]                   [description]
     */
    public function getFoodStoreInfo()
    {
         if(Request::instance()->isPost()){  
          $food = new FoodStore();
          $food_store_id = input('post.food_store_id/d');
          $longitude = input('post.longitude');
          $latitude = input('post.latitude');
          $page = $this->p();    
          $limit = input('post.limit/d');
          if(empty($food_store_id))  return $this->apiReturn('400','美食店铺id为空',0);
          $tourInfo = $food->getFoodStoreInfo($food_store_id,$longitude,$latitude);  
          return $this->apiReturn('200','成功',$tourInfo);
        }

    }

    /**
     *
     * 我的---美食管理
     */
    public function foodManage()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询数据
            $res = Db::name('food_store')->field('food_store_id,food_store_icon,food_store_name,food_tag')->where('uid',$post['uid'])->find();
            
            $res['food_tag'] = explode(',',$res['food_tag']);
            if(empty($res['food_tag'])){
                $res['food_tag'] = [];
            }
            $res['food_store_icon'] = addWaterByField($res['food_store_icon']);
            if($res){
                $res['count'] = Db::name('collect')->where('type',4)->count();
                $this->apiReturn('200','成功',$res);
            }else{
                $this->apiReturn('400','失败');
            }
        }
    }

    /*
     * 我的--店铺信息
     */
    public function foodInfo()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $res = Db::name('food_store')
                ->field('food_store_id,food_store_name,food_store_tel,
                city,food_store_address,app_time,food_people_money,food_store_time,food_tag,food_store_content')
                ->where('food_store_id',$post['food_store_id'])
                ->find();
            //处理时间
            $res['app_time'] = date('Y-m-d',$res['app_time']);
            if($res){
                $this->apiReturn('200','成功',$res);
            }else{
                $this->apiReturn('400','失败');
            }
        }
    }

}